package ar.com.tristeslos3tigres.radioapp_platino1043;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import ar.com.tristeslos3tigres.radioapp_platino1043.adapters.ProgramacionParserDom;
import ar.com.tristeslos3tigres.radioapp_platino1043.entities.Programa;

public class ProgramacionActivity extends ListActivity {

	// private Button btnCargar;
	// private TextView txtResultado;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		AsyncTask<ProgramacionActivity, Integer, List<Programa>> task = new AsyncTask<ProgramacionActivity, Integer, List<Programa>>() {

			ProgramacionActivity activity;

			@Override
			protected List<Programa> doInBackground(
					ProgramacionActivity... params) {
				if (params.length > 0)
					activity = params[0];
				else
					activity = null;

				ProgramacionParserDom saxparser = new ProgramacionParserDom(
						MainActivity.URL_PROGRAMACION);

				List<Programa> programas = saxparser.parse();

				return programas;
			}

			@Override
			protected void onPostExecute(List<Programa> result) {
				super.onPostExecute(result);

				if (activity != null) {
					activity.mostrarProgramas(result);
				}
			}
		};
		task.execute(this);

	}

	private void mostrarProgramas(List<Programa> programas) {
		getListView().setAdapter(new CustomAdapter_iconrowRss(this, programas));
	}

	public class CustomAdapter_iconrowRss extends BaseAdapter implements
			Filterable {

		private Context mContext;
		private List<Programa> list_programas;

		public CustomAdapter_iconrowRss(Context context,
				List<Programa> programaList) {
			// super(context, R.layout.simpleiconrow, list);
			// super();
			mContext = context;
			this.list_programas = programaList;
		}

		public View getView(final int position, View v, ViewGroup parent) {
			View mView = v;
			if (mView == null) {
				LayoutInflater vi = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				mView = vi.inflate(R.layout.iconrow_rss, null);
			}

			TextView tv_nombre = (TextView) mView
					.findViewById(R.id.iconrow_rss_titulo);
			TextView tv_descripcion = (TextView) mView
					.findViewById(R.id.iconrow_rss_descripcion);
			TextView tv_dia = (TextView) mView
					.findViewById(R.id.iconrow_rss_dia);
			TextView tv_hora = (TextView) mView
					.findViewById(R.id.iconrow_rss_hora);

			if (list_programas.get(position) != null) {

				tv_nombre.setText(list_programas.get(position)
						.getNombre_programa());

				tv_descripcion.setTextColor(Color.BLACK);

				tv_dia.setText(list_programas.get(position).getDia());

				String hr_ = list_programas.get(position).getHorario();

				tv_hora.setText(hr_);
				tv_descripcion.setText(list_programas.get(position)
						.getDescripcion_programa());

			}

			return mView;
		}

		public int getCount() {
			return list_programas.size();
		}

		public Object getItem(int position) {
			return list_programas.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		@Override
		public Filter getFilter() {

			return null;
		}

	}
	/*
	 * @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
	 * menu; this adds items to the action bar if it is present.
	 * getMenuInflater().inflate(R.menu.main, menu); return true; }
	 */
}
